package sharetime.playlist.controllers;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import sharetime.playlist.domain.Playlist;
import sharetime.playlist.domain.PlaylistComment;
import sharetime.playlist.service.IPlaylistCommentManager;
import sharetime.playlist.service.IPlaylistManager;
import sharetime.web.controllers.BaseController;

@Controller
@RequestMapping("/playlistcomment")
public class EditPlaylistCommentController extends BaseController {
	
	@Autowired
	@Qualifier("playlistManager")
	private IPlaylistManager playlistManager;
	
	@Autowired
	@Qualifier("playlistCommentManager")
	private IPlaylistCommentManager playlistCommentManager;
	
	@RequestMapping(method = RequestMethod.POST, params = "!_method")
	public void create(@RequestParam long playlistId, @ModelAttribute PlaylistComment pc, 
					   BindingResult br, Model model, HttpServletResponse response){
		if (br.hasErrors()) {

		} else {
			Playlist pl = playlistManager.getPlaylistById(playlistId);
			pc.setPlaylist(pl);
			playlistCommentManager.createPlaylistComment(pc);

			String[] excludeAttributes = { "*.playlist", "*.owner" };
			String[] includeAttributes = {};

			model.addAttribute("msg", "OK");

			renderJson(response, model, excludeAttributes, includeAttributes);
		}
	}
}
